package com.ybbase.framework.controller.flow;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ybbase.framework.base.controller.BaseController;
import com.ybbase.framework.base.model.ResponseBean;
import com.ybbase.framework.common.constant.CommonResultCode;
import com.ybbase.framework.model.po.flow.NodeRecord;
import com.ybbase.framework.service.flow.NodeRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 流程节点记录 前端控制器
 *
 * @author cg
 * @since 2019-12-04
 * 云平台2.0
 */
@RestController
@RequestMapping("/node-record")
@Api("流程节点记录")
public class NodeRecordController extends BaseController {
    private static final Logger LOGGER = LogManager.getLogger(NodeRecordController.class);

    @Autowired
    private NodeRecordService targetService;

    /**
     * 流程节点记录分页查询
     *
     * @param pageIndex 页数
     * @param step      每页数量
     * @return ResponseBean
     * @author cg
     * @date 2019-12-04
     * @since cloud2.0
     */
    @GetMapping("/list")
    @ApiOperation(value = "流程节点记录分页查询", notes = "流程节点记录分页查询", response = ResponseBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "rows", value = "每页行数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "noderecord", value = "流程节点记录", dataType = "NodeRecord对象")
    })
    public ResponseBean findListByPage(@RequestParam(name = "page", defaultValue = "1") int pageIndex,
                                       @RequestParam(name = "rows", defaultValue = "20") int step, NodeRecord noderecord) {
        try {
            Wrapper<NodeRecord> wrapper = new QueryWrapper<NodeRecord>();
            Page<NodeRecord> page = new Page(pageIndex, step);
            targetService.page(page, wrapper);
            return new ResponseBean(9000, "查询成功!", page);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("流程节点记录分页查询异常：", e.getMessage());
            return new ResponseBean(10000, "查询失败!", new Page());
        }
    }


    /**
     * 流程节点记录获取全部数据
     *
     * @return ResponseBean
     * @author cg
     * @date 2019-12-04
     * @since cloud2.0
     */
    @GetMapping("/all")
    @ApiOperation(value = "流程节点记录获取全部数据", notes = "流程节点记录获取全部数据")
    public ResponseBean findAll() {
        try {
            List<NodeRecord> models = targetService.list();
            return new ResponseBean(9000, "查询成功!", models);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("流程节点记录获取全部数据异常：", e.getMessage());
            return new ResponseBean(10000, "查询失败!", new ArrayList<>());
        }
    }

    /**
     * 流程节点记录根据ID查找数据
     *
     * @param id 唯一编码
     * @return ResponseBean
     * @author cg
     * @date 2019-12-04
     * @since cloud2.0
     */
    @GetMapping("/find")
    @ApiOperation(value = "流程节点记录根据ID查找数据", notes = "流程节点记录根据ID查找数据")
    @ApiImplicitParam(required = true, name = "id", value = "唯一编码", dataType = "Long")
    public ResponseBean find(@RequestParam("id") Long id) {
        try {
            NodeRecord noderecord = targetService.getById(id);
            if (noderecord == null) {
                return new ResponseBean(10000, "查询无此记录!", null);
            }
            return new ResponseBean(9000, "查询成功!", noderecord);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("流程节点记录根据ID查找数据异常：", e.getMessage());
            return new ResponseBean(10000, "查询失败!", new NodeRecord());
        }
    }

    /**
     * 流程节点记录保存数据
     *
     * @param noderecord 流程节点记录
     * @return ResponseBean
     * @author cg
     * @date 2019-12-04
     * @since cloud2.0
     */
    @PostMapping("/save")
    @ApiOperation(value = "流程节点记录保存数据", notes = "流程节点记录保存数据")
    @ApiImplicitParam(required = true, name = "noderecord", value = "流程节点记录", dataType = "NodeRecord对象")
    public ResponseBean save(@RequestBody NodeRecord noderecord) {
        try {
            targetService.save(noderecord);
            return new ResponseBean(9000, "插入成功!", noderecord);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("流程节点记录保存数据：", e.getMessage());
            return new ResponseBean(10000, "插入失败!", null);
        }
    }

    /**
     * 流程节点记录更新数据
     *
     * @param noderecord 流程节点记录
     * @return ResponseBean
     * @author cg
     * @date 2019-12-04
     * @since cloud2.0
     */
    @PutMapping("/update")
    @ApiOperation(value = "流程节点记录更新数据", notes = "流程节点记录更新数据")
    @ApiImplicitParam(required = true, name = "noderecord", value = "流程节点记录", dataType = "NodeRecord对象")
    public ResponseBean update(@RequestBody NodeRecord noderecord) {
        try {
            targetService.updateById(noderecord);
            return new ResponseBean(9000, "更新成功!", noderecord);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("流程节点记录更新数据：", e.getMessage());
            return new ResponseBean(10000, "更新失败!", null);
        }
    }

    /**
     * 流程节点记录删除数据
     *
     * @param ids 批量删除数据
     * @return ResponseBean
     * @author cg
     * @date 2019-12-04
     * @since cloud2.0
     */
    @DeleteMapping("/del")
    @ApiOperation(value = "流程节点记录删除数据", notes = "流程节点记录删除数据")
    @ApiImplicitParam(required = true, name = "ids", value = "批量删除数据", dataType = "List<Long>")
    public ResponseBean del(@RequestParam("ids") List<Long> ids) {
        try {
            targetService.removeByIds(ids);
            return new ResponseBean(9000, "删除成功!", null);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("流程节点记录删除数据：", e.getMessage());
            return new ResponseBean(10000, "删除失败!", null);
        }
    }

    /**
     * 根据开户单id查询查询授权人和机构
     *
     * @param business_id 唯一编码
     * @return ResponseBean
     * @author phm
     * @date 2019-12-10
     * @since cloud2.0
     */
    @GetMapping("/getFlowData")
    @ApiOperation(value = "根据开户单id查询查询授权人和机构", notes = "根据开户单id查询查询授权人和机构")
    @ApiImplicitParam(required = true, name = "business_id", value = "开户单id", dataType = "Integer")
    public ResponseBean getFlowData(@RequestParam("business_id") Integer business_id) {
        try {
            List<NodeRecord> list = targetService.getFlowData(business_id);
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(), "根据开户单id查询查询授权人和机构获取数据" + CommonResultCode.SUCCESS.getMsg(), list);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("根据开户单id查询查询授权人和机构异常：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "根据开户单id查询查询授权人和机构" + CommonResultCode.FAIL.getMsg());
        }
    }

    /**
     * 根据开户单id查询查询授权人和机构
     *
     * @param business_id 唯一编码
     * @return ResponseBean
     * @author phm
     * @date 2019-12-10
     * @since cloud2.0
     */
    @GetMapping("/getFlowDataCash")
    @ApiOperation(value = "根据开户单id查询查询授权人和机构", notes = "根据开户单id查询查询授权人和机构")
    @ApiImplicitParam(required = true, name = "business_id", value = "开户单id", dataType = "Integer")
    public ResponseBean getFlowDataCash(@RequestParam("business_id") Integer business_id) {
        try {
            List<NodeRecord> list = targetService.getFlowDataCash(business_id);
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(), "根据开户单id查询查询授权人和机构获取数据" + CommonResultCode.SUCCESS.getMsg(), list);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("根据开户单id查询查询授权人和机构异常：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(), "根据开户单id查询查询授权人和机构" + CommonResultCode.FAIL.getMsg());
        }
    }
}
